package com.apestech.api.xml.esb;

import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.DataMappingUtil;
import com.apestech.framework.util.callback.JSONMapKeyInter;
import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-12-06-11:01
 */
public class XmlArray extends DataMappingUtil {

    private String key;
    private JSONObject jsonValue;
    private String strValue;
    private JSONObject out;
    private JSONArray outlist;
    private JSONObject param;
    private JSONObject modul;
    private String theme;
    private String pkey;
    private ArrayList<String> keyword;

    //补，增加单纯的入参数据构造
    public XmlArray(String key, Object value, JSONObject modul, JSONObject param, String theme){
        this.key=key;
        if(value instanceof JSONObject) jsonValue= (JSONObject) value;
        else strValue= (String) value;
        this.modul=modul;
        this.param=param;
        this.theme=theme;
        out=new JSONObject();
        outlist=new JSONArray();
        keyword=new ArrayList<String>();
        keyword.add("grid");
        keyword.add("panel");
        keyword.add("tabs");
        keyword.add("tab");
    }

    public void expain() throws Exception {
        if(jsonValue!=null){
            getArrayVar();
        }
        if(strValue!=null){
            getControlModul();
        }

        if(jsonValue==null && strValue==null){
            getControlData();
        }
        if(strValue!=null){
            if(out==null) out=new JSONObject();
            if(!isArrayFlag(key)) out.put(key, strValue);
        }

    }

    private void getArrayVar() throws Exception {
        if(jsonValue.get("i_0")!=null) {
            int i = 0;
            String k = "i_" + i;
            Object obj = jsonValue.get(k);
            while(obj!=null){
                getSubCtrl(k);
                if(obj instanceof JSONObject){
                    JSONObject m= (JSONObject) obj;
                    ArrayList<String> klist=m.getKeylist();
                    int klen=klist.size();
                    for(int j=0; j<klen; j++){
                        String key=klist.get(j);
                        XmlArray xml=new XmlArray(key, m.get(key), m, param, theme);
                        xml.setPkey(key);
                        xml.expain();
                        outlist.add(xml.getOut());
                    }
                } else {
                    XmlArray xml=new XmlArray(k, obj, jsonValue, param, theme);
                    xml.setPkey(key);
                    xml.expain();
                    outlist.add(xml.getOut());
                }
                i++;
                k="i_"+i;
                obj=modul.get(k);
            }
            out.put(key, outlist);
        }
    }

    private void getSubCtrl(String sub) throws Exception {
        int i=0;
        String k=sub+"_"+i;
        Object obj=jsonValue.get(k);
        while(obj!=null){
            ID_VAR idv1=getIdVar((String) obj);
            if(keyword.contains(idv1.getId())){
                param.putAll((JSONObject)param.get(idv1.getVar()));
                param.putAll((JSONObject) param.get(idv1.getVar()+"_data"));
                XmlArray xml=new XmlArray(k, idv1.getId(), jsonValue, param, theme);
                xml.setPkey(key);
                xml.expain();
                param.putAll(xml.getParam());
                ID_VAR idv=getIdVar(jsonValue.getString(sub));
                if(idv.getId().equals("tabs")){
                    JSONArray list= (JSONArray) param.get("tab_fields");
                    if(list==null) list=new JSONArray();
                    param.putAll((JSONObject) param.get(idv.getVar()));
                    param.putAll((JSONObject) param.get(idv.getVar()+"_data"));
                    list.add(xml.getOut());
                    param.put("tab_fields", list);
                    param.put("genre",xml.getOut().getString("type"));
                }
            }
            i++;
            k=sub+"_"+i;
            obj=jsonValue.get(k);
        }
    }

    private void getControlModul() throws Exception {
        JSONObject con=null;
        ID_VAR idv=getIdVar(strValue);
        if(keyword.contains(idv.getId()))
            con = EsbUtil.getFormDef(idv.getId(), theme);
        if(con!=null){
            param.putAll((JSONObject) param.get(idv.getVar()));
            param.putAll((JSONObject) param.get(idv.getVar()+"_data"));
            XmlArray xml=new XmlArray(idv.getId(), null, con, param, theme);
            xml.expain();
            if("tab".equals(idv.getId())){
                out.put(idv.getId(), xml.getOut());
                param.put("genre", idv.getId());
            }
            else out.putAll(xml.getOut());
        }
    }

    private void getControlData() throws Exception {
        if(keyword.contains(key)){
            param.put("type",key);
            out.putAll(getOutJson(modul, param, theme, new JSONMapKeyInter(){
                @Override
                public Object getValue(String key, Object value) throws Exception {
                    if(param.get("type").equals("tabs")){
                        if("tabs".equals(key)){
                            XmlArray xml=new XmlArray("tab", null, EsbUtil.getFormDef("tab", theme), param, theme);
                            xml.expain();
                            JSONArray list=new JSONArray();
                            list.add(xml.getOut());
                            return list;
                        }
                    }
                    return null;
                }
            } ));
            param.put(key, out);
        }
    }

    private boolean isArrayFlag(String k){
        if(k==null) return false;
        if(k.indexOf("i_")==0) return true;
        if(k.equals("grid") || k.equals("panel") || k.equals("tabs") || k.equals("tab")) return true;
        return false;
    }

    public JSONObject getOut() {
        return out;
    }

    public JSONArray getOutlist() {
        return outlist;
    }

    public String getPkey() {
        return pkey;
    }

    public void setPkey(String pkey) {
        this.pkey = pkey;
    }

    public JSONObject getParam() {
        return param;
    }
}


